<?php

// 1 普通 2  3  4 分别对应 VP1 VP2 VP3

//        $totalPercent = 3; 总流水百分比
//        $pWinMap = [2 => 1, 3 => 2, 4 => 3];  各级别提成百分比


function p($a) {
    echo '<pre>';
    print_r($a);
}

for($i=1;$i<100;$i++){
    echo '<hr>';
    getApplyMoney(100, [rand(1,4), rand(1,4), rand(1,4), rand(1,4)]);
}

function getApplyMoney($productPrice, $levelList) {
    [$my, $one, $two, $three] = $levelList;//[1,4,3,4]
    $gradeDesc = [1 => '普通会员', 2 => 'vp1', 3 => 'vp2', 4 => 'vp3'];
    echo '我参团的付款金额:'.$productPrice. "<br>";
    echo '我的等级为:'.$gradeDesc[$my]. "<br>";
    echo '我的上级等级为:'.$gradeDesc[$one]. "<br>";
    echo '我的上级的上级等级为:'.$gradeDesc[$two]. "<br>";
    echo '我的上级的上级的上级等级为:'.$gradeDesc[$three]. "<br>";
    $pWinMap = [2 => 1, 3 => 2, 4 => 3];
    $totalPercent = 3;
    $myWinPercent = $pWinMap[$my] ?? 0;
    $oneWinPercent = $pWinMap[$one] ?? 0;
    $twoWinPercent = $pWinMap[$two] ?? 0;
    $threeWinPercent = $pWinMap[$three] ?? 0;
    $myWin = $oneWin = $twoWin = $threeWin = 0;
    if ($myWinPercent > 0) {
        $myWin = bcmul((string)$myWinPercent, (string)round($productPrice / 100, 2), 2);
        echo '我的vip级别奖励:' . $myWin . "<br>";
        $totalPercent -= $myWinPercent;
    } else {
        echo '我的vip级别奖励为0~' . "<br>";
    }

    if ($totalPercent > 0 && $oneWinPercent > 0) {
        $oneWinPercent = ($oneWinPercent - $myWinPercent) > 0 ? ($oneWinPercent - $myWinPercent) : 0;
        $oneWin = bcmul((string)$oneWinPercent, (string)round($productPrice / 100, 2), 2);
        echo '我的上级vip级别奖励:' . $oneWin . "<br>";
        $totalPercent -= $oneWinPercent;
    } else {
        echo '我的上级vip级别奖励为0~' . "<br>";
    }

    if ($totalPercent > 0 && $twoWinPercent > 0) {
        $twoWinPercent = ($twoWinPercent - $oneWinPercent - $myWinPercent) > 0 ? ($twoWinPercent - $oneWinPercent - $myWinPercent) : 0;
        $twoWin = bcmul((string)$twoWinPercent, (string)round($productPrice / 100, 2), 2);
        echo '我的上级的上级vip级别奖励:' . $twoWin . "<br>";
        $totalPercent -= $twoWinPercent;
    } else {
        echo '我的上级的上级vip级别奖励为0~' . "<br>";
    }

    if ($totalPercent > 0 && $threeWinPercent > 0) {
        $threeWinPercent = ($threeWinPercent - $twoWinPercent - $oneWinPercent - $myWinPercent) > 0 ? ($threeWinPercent - $twoWinPercent - $oneWinPercent - $myWinPercent) : 0;
        $threeWin = bcmul((string)$threeWinPercent, (string)round($productPrice / 100, 2), 2);
        echo '我的上级的上级的上级vip级别奖励:' . $threeWin. "<br>";
        $totalPercent -= $threeWinPercent;
    } else {
        echo '我的上级的上级的上级vip级别奖励为0~' . "<br>";
    }
    $winInfo = [$myWin, $oneWin, $twoWin, $threeWin];
    //判断4出现在级别列表的个数
    $countInfo = array_count_values($levelList);
    if (key_exists(4, $countInfo)) {
        if ($countInfo[4] > 1) {
            //至少出现2次 取最近2次的下标提取平级奖励
            $otherWinInfo = [];
            foreach ($levelList as $k => $v) {
                if ($v == 4 && count($otherWinInfo) < 2) {
                    $otherWinInfo[$k] = $winInfo[$k];
                } else if (count($otherWinInfo) > 1) {
                    break;
                }
            }
            $endKeys = array_keys($otherWinInfo)[count($otherWinInfo) - 1];
            $startKeys = array_keys($otherWinInfo)[0];
            if ($endKeys == 1) {
                echo '我的上级抽取V3平级流水' . round($winInfo[$startKeys] * 10 / 100, 2). "<br>";
            } else if ($endKeys == 2) {
                echo '我的上级的上级抽取V3平级流水' . round($winInfo[$startKeys] * 10 / 100, 2). "<br>";
            } else if ($endKeys == 3) {
                echo '我的上级的上级的上级抽取V3平级流水' . round($winInfo[$startKeys] * 10 / 100, 2). "<br>";
            }
        }
    } else {
        return true;
    }

}